package com.project.internship.controller.practice;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.internship.entity.*;
import com.project.internship.entity.Class;
import com.project.internship.entity.resp.RespBean;
import com.project.internship.entity.resp.RespPageBean;
import com.project.internship.service.*;
import com.project.internship.vo.StudentDeptMajorClassVO;
import com.project.internship.vo.TeacherDeptVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学院端实习分配
 */
@RestController
@RequestMapping("/practice/allot")
public class PracticeAllotController {

    @Autowired
    BasesService basesService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    MajorService majorService;
    @Autowired
    ClassService classService;
    @Autowired
    StudentService studentService;
    @Autowired
    AllotService allotService;

    @GetMapping("/base")
    public List<Bases> getNonAllotBases(){
        return basesService.getNonAllotBases();
    }

    /**
     * 获得本学院所有教师的id，name
     * @return
     */
    @GetMapping("/teachers")
    public List<Teacher> getNonTeacherName(){
        return teacherService.getNonTeacherName();
    }

    /**
     * 获得本学院所有专业的id，name
     * @return
     */
    @GetMapping("/majors")
    public List<Major> getMajors(){
        return majorService.getMajors();
    }

    /**
     * 获得本专业的所有班级的id，name
     * @return
     */
    @GetMapping("/classes/{id}")
    public List<Class> getClasses(@PathVariable("id") Integer id){
        return classService.getClasses(id);
    }

    /**
     * 获得本班级的所有学生id，studentId，studentId+' '+name
     * @param id
     * @return
     */
    @GetMapping("/students/{id}")
    public List<Student> getStudents(@PathVariable("id") Integer id){
        return studentService.getStudents(id);
    }

    @GetMapping("/allot_bases")
    public RespPageBean getAllotBases(@RequestParam(defaultValue = "1") Integer current,
                                      @RequestParam(defaultValue = "10") Integer size,
                                      String keyword){
        Page<Bases> page = new Page<>(current,size);

        Long total = basesService.getAllotBases(page, keyword).getTotal();
        List<Bases> data = basesService.getAllotBases(page, keyword).getRecords();

        return new RespPageBean(total, data);
    }

    /**
     * 获取分配到该基地的教师信息
     * @return
     */
    @GetMapping("/baseTeacher/{bid}")
    public List<Teacher> getBaseTeacher(@PathVariable("bid") Integer bid){
        return teacherService.getBaseTeacher(bid);
    }

    /**
     * 获取分配到该基地的学生信息
     * @param
     * @return
     */
    @GetMapping("/base_student/{bid}")
    public RespPageBean getBaseStudent( @PathVariable("bid") Integer bid,
                                        @RequestParam(defaultValue = "1") Integer current,
                                        @RequestParam(defaultValue = "10") Integer size
                                       ){
        Page<StudentDeptMajorClassVO> page = new Page<>(current, size);
        Long total = allotService.getBaseStudent(page, bid).getTotal();
        List<StudentDeptMajorClassVO> data = allotService.getBaseStudent(page, bid).getRecords();

        return new RespPageBean(total, data);

    }

    @PostMapping("/add")
    public RespBean addAllot(Integer bid, Integer[] tids, Integer[] sids){
        if(allotService.addAllot(bid, tids, sids) == (tids.length * sids.length)) {
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @PutMapping("/update")
    public RespBean updateAllot(Integer bid, Integer[] tids, Integer[] sids){
        if(allotService.updateAllot(bid, tids, sids) == (tids.length * sids.length)) {
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @DeleteMapping("/delete/{bid}")
    public RespBean deleteAllot(@PathVariable("bid") Integer bid){
        if(allotService.deleteAllot(bid) != 0) {
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败");
    }


}
